# _*_ coding : utf-8 _*_
# @Time : 2023/3/16 0016 13:14
# @Author : 菜鸟王小二
# @File : 18_xpath和urllib的综合使用
# @Project : python爬虫

import urllib.request
from lxml import etree

url = 'https://www.baidu.com'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63'
}

request = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
# etree.HTML()方法解析服务器响应的文件
tree = etree.HTML(content)

text = tree.xpath('//input[@id="su"]/@value')[0]
print(text)